
-----------------------------------------------------------
-- UI control

-----------------------------------------------------------
--鼠标事件-
LoveMouseType = {
	LB = "l", MB = "m", RB = "r",		--左，中，右键-
	WHEEL_UP = "wu", WHEEL_DOWN = "wd",	--滚轮，上下-
	X1 = "x1", X2 = "x2",				--button 4, button 5
};
UIMouseType = {
	LB = 0, MB = 1, RB = 2,			--左，中，右键-
	WHEEL_UP = 3, WHEEL_DOWN = 4,	--滚轮，上下-
	X1 = 5, X2 = 6,					--button 4, button 5
};

function LoveMouseTypeToUIMouseType(button)
	if button == LoveMouseType.LB then
		return UIMouseType.LB;
	elseif button == LoveMouseType.MB then
		return UIMouseType.MB;
	elseif button == LoveMouseType.RB then
		return UIMouseType.RB;
	elseif button == LoveMouseType.WHEEL_UP then
		return UIMouseType.WHEEL_UP;
	elseif button == LoveMouseType.WHEEL_DOWN then
		return UIMouseType.WHEEL_DOWN;
	elseif button == LoveMouseType.X1 then
		return UIMouseType.X1;
	elseif button == LoveMouseType.X2 then
		return UIMouseType.X2;
	end
	return nil;
end

------------------------------------------------------------
--UI-
UI = {};
UI.__index = UI;

function UI:new()
	local ui = {};
	setmetatable(ui, self);
	ui.control = {};
	return ui;
end

function UI:draw()
	for i,ctrl in pairs(self.control) do
		if ctrl.visible then
			ctrl:draw();
		end
	end
end

function UI:update(dt)
	local x = love.mouse.getX();
	local y = love.mouse.getY();
	for i,ctrl in pairs(self.control) do
		ctrl:update(dt);
		if not ctrl.disable and ctrl.mouse_event then
			ctrl:mousemove(x, y);
		end
	end
end

function UI:mousepressed(x, y, button)
	local ui_button = LoveMouseTypeToUIMouseType(button);
	for i,ctrl in pairs(self.control) do
		if not ctrl.disable and ctrl.mouse_event then
			ctrl:mousepressed(x, y, ui_button);
		end
	end
end

function UI:mousereleased(x, y, button)
	local ui_button = LoveMouseTypeToUIMouseType(button);
	for i,ctrl in pairs(self.control) do
		if not ctrl.disable and ctrl.mouse_event then
			ctrl:mousereleased(x, y, ui_button);
		end
	end
end

------------------------------------------------------------
--声音资源-
Audios = {}
function Audios:create(idx, source, mode)
	if mode==nil then
		Audios[idx] = love.audio.newSource(source);
	else
		Audios[idx] = love.audio.newSource(source, mode);
	end
end

function Audios:Pause()
	love.audio.pause();
end

function Audios:resume()
	love.audio.resume();
end

function Audios:Stop()
	love.audio.stop();
end

function Audios:rewind()
	love.audio.rewind();
end

------------------------------------------------------------
--图片资源-
Images = {
	list={},
};

function Images:create(idx,path)
	self.list[idx] = love.graphics.newImage(path);
end

function Images:getWidth(idx)
	return self.list[idx]:getWidth();
end

function Images:getHeight(idx)
	return self.list[idx]:getHeight();
end

function Images:draw(idx, x, y)
	love.graphics.setColor(255,255,255,255);
	love.graphics.draw(self.list[idx], x, y);
end

function Images:draw(idx, x, y, r, sx, sy, ox, oy)
	love.graphics.setColor(255,255,255,255);
	love.graphics.draw(self.list[idx], x, y, r, sx, sy, ox, oy);
end

--绘制图片一部分-
function Images:drawq(idx, x, y, startx,starty, width, height)
	local quad = love.graphics.newQuad(startx,starty,width,height,
		Images:getWidth(idx),Images:getWidth(idx));
	love.graphics.setColor(255,255,255,255);
	love.graphics.drawq(self.list[idx], quad, x, y);
end

-----------------------------------------------------------
--字体资源-
Fonts = {
list={}
};
function Fonts:create(idx, size)
	self.list[idx] = love.graphics.newFont(size);
end

function Fonts:create(idx, fontpath,size)
	self.list[idx] = love.graphics.newFont(fontpath,size);
end

function Fonts:setFont(index)
	love.graphics.setFont(self.list[index]);
end

-----------------------------------------------------------
-- UI 控件基类-
UIControl ={
	x = 0, y = 0,
	visible = true,
	disable = false,		--影响 mousemove 即以下事件-
	keyboard_event = false,	--影响 keypressed keyreleased-
	mouse_event = false,	--影响 mousepressed mouserelease-
	joystick_event = false,	--影响 joystickpressed joystickpressed-
}
UIControl.__index = UIControl

function UIControl:new(o)
	o = o or {};
	setmetatable(o, self);
	o.update = function (dt) end;
	return o;
end

function UIControl:show()
	self.visible = true;
end

function UIControl:hide()
	self.visible = false;
end

-----------------------------------------------------------
--静态文本-
StaticText = UIControl:new({color = {0,0,0}});
StaticText.__index = StaticText;

function StaticText:new(text,font_idx,x,y)
	local st = {};
	setmetatable(st, self);
	st.text = text;
	st.font_idx = font_idx;
	st.x = x;
	st.y = y;
	st.width = Fonts.list[font_idx]:getWidth(text);
	st.height = Fonts.list[font_idx]:getHeight();
	return st;
end

function StaticText:setText(text, font_idx)
	self.text = text;
	if font_idx then
		self.font_idx = font_idx;
	end
	self.width = Fonts.list[self.font_idx]:getWidth(self.text);
	self.height = Fonts.list[self.font_idx]:getHeight();
end

function StaticText:draw()
	Fonts:setFont(self.font_idx);
	love.graphics.setColor(self.color);
	love.graphics.print(self.text, self.x, self.y);
end

-----------------------------------------------------------
--文本按钮-
TextBtn = UIControl:new({
	hover = false,
	click = false,
	normal_color = {0,0,0},
	hover_color = {50,50,50},
	click_color = {0,0,0},
	disable_color = {125,125,125},
	mousepressed_callback = nil,
	mousereleased_callback = nil,
});
TextBtn.__index = TextBtn;

function TextBtn:new(text,font_idx,x,y)
	local newbtn = {};
	setmetatable(newbtn,self);

	newbtn.text = text;
	newbtn.font_idx = font_idx;
	newbtn.x = x;
	newbtn.y = y;
	newbtn.mouse_event = true;
	newbtn.width = Fonts.list[font_idx]:getWidth(text);
	newbtn.height = Fonts.list[font_idx]:getHeight();
	return newbtn;
end

function TextBtn:draw()
	Fonts:setFont(self.font_idx);

	local drawcolor;
	if self.disable then
		drawcolor = self.disable_color;
	elseif self.click then
		drawcolor = self.click_color;
	elseif self.hover then
		drawcolor = self.hover_color;
	else
		drawcolor = self.normal_color;
	end

	love.graphics.setColor(drawcolor);
	love.graphics.print(self.text, self.x, self.y);
end

function TextBtn:mousemove(x, y)
	if x > self.x
		and x < self.x + self.width
		and y < self.y + self.height
		and y > self.y
	then
		self.hover = true;
	else
		self.hover = false;
end
end

function TextBtn:mousepressed(x, y, button)
	if self.hover and UIMouseType.LB==button then
		self.click = true;
		if type(self.mousepressed_callback)=="function" then
			self.mousepressed_callback(x, y, button);
		end
	else
		self.click = false;
	end
end

function TextBtn:mousereleased(x, y, button)
	if self.click then
		self.click = false;
		if type(self.mousereleased_callback)=="function" then
			self.mousereleased_callback(x, y, button);
		end
	end
end

-----------------------------------------------------------
--图片按钮-
ImageBtn = UIControl:new({
	hover = false,
	click = false,
	normal_color = {255,255,255},
	hover_color = {255,255,255},
	click_color = {255,255,255},
	disable_color = {125,125,125},
	mousepressed_callback = nil,
	mousereleased_callback = nil,
});
ImageBtn.__index = ImageBtn;

function ImageBtn:new(img_idx,x,y,width,height)
	local newbtn = {};
	setmetatable(newbtn,self);

	newbtn.normal_img_idx = img_idx;
	newbtn.hover_img_idx = img_idx;
	newbtn.click_img_idx = img_idx;
	newbtn.disable_img_idx = img_idx;
	newbtn.x = x;
	newbtn.y = y;
	newbtn.mouse_event = true;
	if width~=nil then
		newbtn.width = width;
	else
		newbtn.width = Images:getWidth(img_idx);
	end
	if height~=nil then
		newbtn.height = height;
	else
		newbtn.height = Images:getHeight(img_idx);
	end
	return newbtn;
end

function ImageBtn:draw()
	local img_idx;
	local drawcolor;
	if self.disable then
		img_idx = self.disable_img_idx;
		drawcolor = self.disable_color;
	elseif self.click then
		img_idx = self.click_img_idx;
		drawcolor = self.click_color;
	elseif self.hover then
		img_idx = self.hover_img_idx;
		drawcolor = self.hover_color;
	else
		img_idx = self.normal_img_idx;
		drawcolor = self.normal_color;
	end

	love.graphics.setColor(drawcolor);
	love.graphics.draw(Images.list[img_idx], self.x, self.y, 0,
	self.width/Images:getWidth(img_idx), self.height/Images:getHeight(img_idx));
end

function ImageBtn:mousemove(x, y)
	if x > self.x
		and x < self.x + self.width
		and y < self.y + self.height
		and y > self.y
	then
		self.hover = true;
	else
		self.hover = false;
	end
end

function ImageBtn:mousepressed(x, y, button)
	if self.hover and UIMouseType.LB==button then
		self.click = true;
		if type(self.mousepressed_callback)=="function" then
			self.mousepressed_callback(x, y, button);
		end
	else
		self.click = false;
	end
end

function ImageBtn:mousereleased(x, y, button)
	if self.click then
		self.click = false;
		if type(self.mousereleased_callback)=="function" then
			self.mousereleased_callback(x, y, button);
		end
	end
end
